package com.hex.es.aop;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: aop公共抽象超类
 * @author: Hex
 * @date: 2019/3/13 16:29
 */
public abstract class AbstractEsAop {

    protected static final ThreadLocal<Map<String,Object>> PARAM_THREAD_LOCAL = new ThreadLocal<>();

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    private final static String UNDERLINE = "_";

    /***
     * 驼峰命名转为下划线命名
     *
     * @param para
     *        驼峰命名的字符串
     */

    protected static String humpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;
        if (!para.contains(UNDERLINE)) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.insert(i + temp, UNDERLINE);
                    temp += 1;
                }
            }
        } else {
            return para;
        }
        return sb.toString().toLowerCase();
    }

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    protected static String lineToHump(String str) {
        if (!str.contains(UNDERLINE)) {
            return str;
        }
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
